package solutions;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-02-06
 * Time: 15:56
 */

//https://leetcode.cn/problems/reverse-words-in-a-string-iii/submissions/399875837/
class Solution3 {
    public String reverseWords(String s) {
        String[] strs = s.split(" ");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strs.length - 1; ++i) {
            StringBuilder temp = new StringBuilder(strs[i]);
            temp.reverse();
            sb.append(temp).append(" ");
        }
        sb.append(new StringBuilder(strs[strs.length - 1]).reverse());
        return sb.toString();
    }
}

//class Solution {
//    public String reverseWords(String s) {
//        char[] chars = s.toCharArray();
//        int left = 0;
//        int right = 0;
//        while (right < chars.length) {
//            while (right < chars.length && chars[right] != ' ') {
//                ++right;
//            }
//            swap(chars, left, right - 1);
//            left = ++right;
//        }
//        if (left < chars.length) {
//            swap(chars, left, chars.length - 1);
//        }
//        return new String(chars);
//    }
//
//    private void swap(char[] s, int i, int j) {
//        while (i < j) {
//            char temp = s[i];
//            s[i] = s[j];
//            s[j] = temp;
//            ++i;
//            --j;
//        }
//    }
//}
